public class ListDemo {
    public static void main(String[] args) {
        Node n1 = new Node(1, "宋江");
        Node n2 = new Node(2, "吴用");
        Node n3 = new Node(3, "卢俊义");
        Node n4 = new Node(4, "公孙胜");
        Node n41 = new Node(4, "公孙胜1");
        Node n5 = new Node(5, "林冲");

        //添加
        singleLinkedList list = new singleLinkedList();
        list.add1(n4);
        list.add1(n3);
        list.add1(n2);
        list.add1(n1);
        list.delete(n5);
        list.delete(n3);

        list.list();

    }

}
//设计链表基本结构
class singleLinkedList {
    //初始化一个头节点
    private Node head = new Node (0,"");

    public singleLinkedList() {
    }

    //添加节点到单向链表
    //当不考虑编号顺序时候
    //1. 找到最后一个节点
    //2. 将最后的节点next指向新的节点
    public void add(Node newNode){
        Node temp = head;
        //遍历 找到最后
        while(true){
            //找到链表最后
            if(temp.next==null){
                break;
            }
            //如果没有找到最后,就将temp后移
            temp = temp.next;

        }
        //退出while循环时候 temp就指向链表最后
        temp.next = newNode;
    }
    public void add1(Node newNode){
        Node temp = head;
        boolean flag = false;//判断是否已经存在同编号
        while (true){
            if(temp.next==null||temp.next.no>newNode.no){
                break;
            }else if(temp.next.no==newNode.no){
                flag=true;
                break;
            }
            temp=temp.next;

        }
        if(flag){
            System.out.printf("准备插入的节点%d已存在\n",newNode.no);
        }else{
            newNode.next=temp.next;
            temp.next=newNode;
        }

    }
    //显示链表 遍历
    public void list(){
        //判空
        if(head.next==null){
            return;
        }
        //因为头节点不能动 需要一个辅助变量
        Node temp = head.next;
        while(true){
            //是否到链表最后
            if(temp==null){
                break;
            }
            //输出节点信息
            System.out.println(temp);
            //将temp后移,
            temp=temp.next;
        }

    }

    public Node reverseList(Node head) {
        Node re= new Node(0);
        Node cur = head;
        Node next = null;
        if(head.next==null){
            return head;
        }
        while(cur.next!=null){
            next=cur.next;
            next.next=re.next;
            re.next=next.next;
            cur=cur.next;
        }
        return re.next;

    }


    public void update(Node newNode){
        if(head.next==null){
            System.out.println("链表为空");
        }
        //找到需要修改的节点
        Node temp = head;
        boolean flag = false;//判断是否已经存在同编号
        while (true){
            if(temp==null){
                break;
            }else if(temp.no==newNode.no){
                flag=true;
                break;
            }
            temp=temp.next;
        }
        if(flag){
            temp.name= newNode.name;
        }else{
            System.out.println("找不到对应节点");
        }

    }

    //删除
    public void delete(Node newNode){
        Node temp = head;
        boolean flag = false;
        while(true){
            if(temp.next==null){
                break;
            }
            if(temp.next.no==newNode.no){
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if(flag){
            temp.next=temp.next.next;
        }else{
            System.out.printf("找不到%d节点\n",newNode.no);
        }

    }
}

//单个节点(构造方法 toString)
class Node{
    int no;
    String name;
    Node next;

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Node getNext() {
        return next;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public Node(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "Node{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}